<?php
/**
 * Represents a javascript based compare validator.
 */
fily_XMLConverter::registerNamespace('comparevalidator', fily_CompareValidator);
class fily_CompareValidator extends fily_Validator {
    /**
     * Second fily_Control for comparing.
     * @var fily_Control
     */
    protected $controlToValidate2;

    /**
     *
     */
    public function __construct() {
        parent::__construct();
        $this->setErrorMessage('*');

    }

    /**
     * perpares the code for the javascript output
     */
    public function prepare() {
        parent::prepare();
        if($this->getControlToValidate()!=null&&$this->getControlToValidate2()!=null) {
           $code = sprintf("firelynx.CompareValidator('%s', '%s', '%s', '%s', %s, '%s');", $this->getId(), $this->getControlToValidate()->getId(), $this->validationGroup,  $this->getDisplayStyle(),$this->getExecutionTime() , $this->controlToValidate2->getId());
            fily_ClientCodeManager::addCode($code);
            fily_ClientCodeManager::useId($this->controlToValidate->getId());
            fily_ClientCodeManager::useId($this->controlToValidate2->getId());
            fily_ClientCodeManager::useId($this->getId());

        }

    }

    /**
     * sets the second control for comparing
     * @param fily_Control $controlToValidate
     */
    public function setControlToValidate2(fily_Control $controlToValidate) {
        $this->controlToValidate2 = $controlToValidate;
    }

    /**
     * get the second control for comparing
     * @return fily_Control
     */
    public function getControlToValidate2() {
        return $this->controlToValidate2;
    }

    public function XMLAttributes(array $attributes, $innerValue) {
        foreach($attributes as $key => $value) {
            switch ($key) {
                case 'controltovalidate2':
                    $this->lateBindingObjects[$key] = $value;
                    unset($attributes[$key]);
                    break;
            }
        }
        // clear array
        parent::XMLAttributes($attributes, $innerValue);
    }

    public function XMLLateBinding(fily_ControlToHtml $baseControl) {
        $deleteArray = array();
        foreach($this->lateBindingObjects as $key => $value) {
            switch ($key) {
                case 'controltovalidate2':
                  $control = fily_XMLConverter::findControl($value, $this, $baseControl);

                    if($control!=null) {
                        $this->setControlToValidate2($control);
                    }
                    unset($this->lateBindingObjects[$key]);
                    break;
            }
        }
        parent::XMLLateBinding($baseControl);
    }


    

}
?>
